package com.cheetah.start.common.base.result;

import com.cheetah.start.common.enums.BusinessTypeEnum;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * Describe: 统一响应 Controller CreateTime: 2019/10/23
 *
 * @author xjh
 */
@CrossOrigin
public class ResultController {

    /**
     * 成功操作
     */
    public <T> Result<T> success() {
        return Result.success();
    }

    /**
     * 成功操作
     */
    public <T> Result<T> success(String msg) {
        return Result.success(msg);
    }

    /**
     * 成功操作
     */
    public <T> Result<T> success(T data) {
        return Result.success(data);
    }

    /**
     * 成功操作
     */
    public <T> Result<T> success(String msg, T data) {
        return Result.success(msg, data);
    }

    /**
     * 成功操作
     */
    public <T> Result<T> success(int code, String message, T data) {
        return Result.success(code, message, data);
    }

    /**
     * 失败操作
     */
    public <T> Result<T> failure() {
        return Result.failure();
    }

    /**
     * 失败操作
     */
    public <T> Result<T> failure(String msg) {
        return Result.failure(msg);
    }

    /**
     * 失败操作
     */
    public <T> Result<T> failure(String msg, T data) {
        return Result.failure(msg, data);
    }

    /**
     * 失败操作
     */
    public <T> Result<T> failure(int code, String msg, T data) {
        return Result.failure(code, msg, data);
    }

    /**
     * 选择返回
     */
    public <T> Result<T> decide(Boolean b) {
        return Result.decide(b);
    }

    public <T> Result<T> decide(Boolean b, BusinessTypeEnum type) {
        if (type.name().equals(BusinessTypeEnum.ADD.name())) {
            return Result.decide(b, "添加成功", "添加失败");
        } else if (type.name().equals(BusinessTypeEnum.EDIT.name())) {
            return Result.decide(b, "更新成功", "更新失败");
        } else if (type.name().equals(BusinessTypeEnum.REMOVE.name())) {
            return Result.decide(b, "删除成功", "不存在或已被删除");
        }
        return Result.decide(b);
    }

    /**
     * 选择返回
     */
    public <T> Result<T> decide(Boolean b, String success, String failure) {
        return Result.decide(b, success, failure);
    }

    /**
     * 选择返回
     */
    public <T> Result<T> decide(int result) {
        return Result.decide(result > 0);
    }

    public <T> Result<T> decide(int b, BusinessTypeEnum type) {
        if (type.name().equals(BusinessTypeEnum.ADD.name())) {
            return Result.decide(b > 0, "添加成功", "添加失败");
        } else if (type.name().equals(BusinessTypeEnum.EDIT.name())) {
            return Result.decide(b > 0, "更新成功", "更新失败");
        } else if (type.name().equals(BusinessTypeEnum.REMOVE.name())) {
            return Result.decide(b > 0, "删除成功", "不存在或已被删除");
        }
        return Result.decide(b > 0);
    }


    /**
     * 选择返回
     */
    public <T> Result<T> decide(int result, String success, String failure) {
        if (result > 0) {
            return Result.decide(true, success, failure);
        } else {
            return Result.decide(false, success, failure);
        }
    }

    /**
     * 页面跳转
     */
    public ModelAndView jumpPage(String path) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(path);
        return modelAndView;
    }

    /**
     * 带参数的页面跳转
     */
    public ModelAndView jumpPage(String path, Map<String, ?> params) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(path);
        modelAndView.addAllObjects(params);
        return modelAndView;
    }

    /**
     * Describe: 返回 Tree 数据 Param data Return Tree数据
     */
    protected static ResultTree dataTree(Object data) {
        ResultTree resuTree = new ResultTree();
        resuTree.setData(data);
        return resuTree;
    }

    /**
     * Describe: 返回数据表格数据 分页 Param data Return 表格分页数据
     */
    protected static ResultTable pageTable(Object data, long count) {
        return ResultTable.pageTable(count, data);
    }

    /**
     * Describe: 返回数据表格数据 Param data Return 表格分页数据
     */
    protected static ResultTable dataTable(Object data) {
        return ResultTable.dataTable(data);
    }

    /**
     * Describe: 返回树状表格数据 分页 Param data Return 表格分页数据
     */
    protected static ResultTable treeTable(Object data) {
        return ResultTable.dataTable(data);
    }

}
